var express = require('express');
var router = express.Router();
var request = require('request');
var md5 = require('md5');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);

// 首页 列表
router.get('/jkyx/pc/home_list', function(req, res, next) {

    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_home_list,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var homes = results;
				var goods_len = homes.length;
				var goods_ids = '';
				if (goods_len > 0) {
					for (var i = 0; i < goods_len; i ++) {
						var goodsIDS = homes[i].goods_ids;
						goods_ids = goods_ids + goodsIDS + ',';
					}
					goods_ids = goods_ids.substr(0,goods_ids.length - 1);
					getCartList(goods_ids,res,homes);
				}else{
					res.end(JSON.stringify({
		     			msg: '操作成功',
		     			status:'100',
		     			home_list: [],
		     		}));	
				}
			}
		})
	})
});

// 获取购物车中的商品列表
function getCartList(goods_ids,res,homes){
	var selGoods = 'select * from goods where id in (' + goods_ids + '); select * from goods_spec where goods_id in (' + goods_ids + ');';
	pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}else{
			connection.query(selGoods,function (err, results) {
				connection.release();
				if (err){
				  	res.end(JSON.stringify({
		     			msg: '数据库查询失败',
		     			status:'103',
		     			err: err
		     		}));
				}else{
					// 处理商品 和 规格
					var goods = results[0];
					var goods_len = goods.length;
					var specs = results[1];
					var spec_len = specs.length;
					
					for (var i = 0; i < goods_len; i ++) {
						var salecount = 0;
						var spec = [];
						for (var j = 0; j < spec_len; j ++) {
							if (specs[j].goods_id == goods[i].id) {
								salecount = salecount + specs[j].salecount;
								spec.push({
									id: specs[j].id,
									salecount: specs[j].salecount,
									price: specs[j].price / 100,
									spec: specs[j].spec,
									origprice: specs[j].origprice,
									erpcode: specs[j].erpcode,
									stock: specs[j].stock,
								})
							}
						}
						goods[i].salecount = salecount;
						goods[i].spec = spec;
						goods[i].imgs = JSON.parse(goods[i].imgs);
					}
					var homes_len = homes.length;
					var home_list = [];
					if (homes_len > 0) {
						for (var i = 0; i < homes_len; i ++) {
							var goodsIDS = homes[i].goods_ids;
							if (goodsIDS.indexOf(',') == 1) {
								var temps = goodsIDS.split(',');
								var tempGoods = [];
								for (var j = 0; j < temps.length; j ++) {
									for (var k = 0; k < goods_len; k ++) {
										if (temps[j] == goods[k].id) {
											tempGoods.push(goods[k])
										}
									}
								}
								var item = {
									id: homes[i].id,
									index_num: homes[i].index_num,
									image_url: homes[i].image_url,
									goods_ids: homes[i].goods_ids,
									goods: tempGoods
								}
								home_list.push(item);
							} else{
								var tempGoods = [];
								for (var k = 0; k < goods_len; k ++) {
									if (temps[j] == goods[k].id) {
										tempGoods.push(goods[k])
									}
								}
								var item = {
									id: homes[i].id,
									index_num: homes[i].index_num,
									image_url: homes[i].image_url,
									goods_ids: homes[i].goods_ids,
									goods: tempGoods
								}
								home_list.push(item);
							}
						}	
					} 
					res.end(JSON.stringify({
		     			msg: '操作成功',
		     			status:'100',
		     			home_list: home_list,
		     		}));	  
				}
			})
		}
	})
}


// 新建首页
router.get('/jkyx/pc/home_add', function(req, res, next) {
	var param = req.query || req.params; 
	// 轮播图类型  图片地址
	var image_url = param.image_url || '';
	// 商品ID
	var goods_ids = param.goods_ids || '';
	// 序号
	var index_num = param.index_num || '';
	// 时间
	var create_time = CommonJS.getNowFormatDate();
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_home_add,[index_num,image_url,goods_ids,create_time],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));
			}
		})
	})
});

// 编辑首页
router.get('/jkyx/pc/home_edit', function(req, res, next) {
	var param = req.query || req.params; 
	// 轮播图类型  图片地址
	var image_url = param.image_url || '';
	// 商品ID
	var goods_ids = param.goods_ids || '';
	// 序号
	var index_num = param.index_num || '';
	// id
	var id = param.id || '';
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_home_edit,[index_num,image_url,goods_ids,id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));
			}
		})
	})
});

// 删除
router.get('/jkyx/pc/home_delete', function(req, res, next) {
	var param = req.query || req.params; 
	var id = param.id || 1;
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.pc_home_delete,[id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			banners: results[1]
	     		}));
			}
		})
	})
});

module.exports = router;
